// db.ts
import Dexie, { type EntityTable } from 'dexie';

export interface Users {
    username: string;
    password: string;
    enabled: number;
    realName?: string;
    avatar?: string;
    joinDate?: string;
    usersTypeId: number;
}

export interface UsersType {
    usersTypeId?: number;
    usersTypeName: string;
}

export interface School {
    schoolId?: number;
    schoolName: string;
    schoolIsDel?: number;
}

export interface Student {
    studentId?: number;
    studentNumber: string;
    phoneNumber: string;
    qq?: string;
    weixin?: string;
    schoolId: number;
    username: string;
}

export interface Organize {
    organizeId?: number;
    responsiblePerson: string;
    responsiblePersonPhoneNumber: string;
    address: string;
    businessLicense: string;
    username: string;
}

export interface SalvationType {
    salvationTypeId?: number;
    salvationTypeName: string;
}

export interface OrganizeSalvationType {
    organizeId: number;
    salvationTypeId: number;
}

export interface Salvation {
    salvationId: string;
    salvationTypeId: number;
    petSituation: string;
    username: string;
    createDate: string;
    address: string;
    phoneNumber: string;
    qq?: string;
    weixin?: string;
    salvationStatus: number;
}

export interface PetImage {
    petImageId: string;
    petImageUrl: string;
    salvationId: string;
}

export interface PetSalvationPlan {
    petSalvationPlanId: string;
    node: string;
    situation?: string;
    organizeId?: number;
    salvationId: string;
    petSalvationPlanDate: string;
}

export interface PlatformInformation {
    platformId: string;
    companyName: string;
    responsiblePerson: string;
    responsiblePersonPhoneNumber: string;
    companyAddress: string;
    postalCode: string;
    bank: string;
    bankAccount: string;
}

export interface DonationType {
    donationTypeId?: number;
    donationTypeName: string;
}

export interface DonationSubdivisionType {
    donationSubdivisionTypeId?: number;
    donationSubdivisionTypeName: string;
}

export interface Donation {
    donationId: string;
    donationTypeId: number;
    donationSubdivisionTypeId: number;
    money?: number;
    goodsName?: string;
    goodsNumber?: number;
    donationDate?: string;
    donor?: string;
    donorPhoneNumber?: string;
    username?: string;
    salvationId?: string;
    postalStatus: number;
    payStatus: number;
    purpose: string;
}

export interface Comment {
    commentId: string;
    commentContent: string;
    salvationId: string;
    username: string;
    commentDate: string;
}

export interface Collection {
    collectionId: string;
    salvationId: string;
    collectionDate: string;
    remarks?: string;
    username: string;
}

export interface OrganizeDetail {
    organizeId: number;
    content: string;
}

export interface OrganizeImages {
    organizeId: number;
    imageUrl: string;
}

export interface SystemConfig {
    configKey: string;
    configValue: string;
}


export class MySubClassedDexie extends Dexie {
    // 'friends' is added by dexie when declaring the stores()
    // We just tell the typing system this is the case
    users!: EntityTable<Users>;
    usersType!: EntityTable<UsersType>;
    school!: EntityTable<School>;
    student!: EntityTable<Student>;
    organize!: EntityTable<Organize>;
    salvationType!: EntityTable<SalvationType>;
    organizeSalvationType!: EntityTable<OrganizeSalvationType>;
    salvation!: EntityTable<Salvation>;
    petImage!: EntityTable<PetImage>;
    petSalvationPlan!: EntityTable<PetSalvationPlan>;
    platformInformation!: EntityTable<PlatformInformation>;
    donationType!: EntityTable<DonationType>;
    donationSubdivisionType!: EntityTable<DonationSubdivisionType>;
    donation!: EntityTable<Donation>;
    comment!: EntityTable<Comment>;
    collection!: EntityTable<Collection>;
    systemConfig!: EntityTable<SystemConfig>;
    organizeDetail!: EntityTable<OrganizeDetail>;
    organizeImages!: EntityTable<OrganizeImages>;

    constructor() {
        super('salvation');
        this.version(1).stores({
            users: '&username',
            usersType: '++usersTypeId',
            school: '++schoolId, schoolIsDel',
            student: '++studentId, phoneNumber, schoolId, username, [schoolId+studentNumber]',
            organize: '++organizeId, responsiblePersonPhoneNumber, username',
            salvationType: '++salvationTypeId',
            organizeSalvationType: '[organizeId+salvationTypeId], organizeId, salvationTypeId',
            salvation: '&salvationId, username, createDate',
            petImage: '&petImageId, salvationId',
            petSalvationPlan: '&petSalvationPlanId, organizeId, salvationId',
            platformInformation: '&platformId',
            donationType: '++donationTypeId',
            donationSubdivisionType: '++donationSubdivisionTypeId',
            donation: '&donationId, username, salvationId',
            comment: '&commentId, username, salvationId',
            collection: '&collectionId, username, salvationId',
            systemConfig: '&configKey',
            organizeDetail: '&organizeId',
            organizeImages: '&organizeId'
        });
    }
}

export const db = new MySubClassedDexie();